/*
 * TLSLink.cpp
 *
 *  Created on: 30/10/2011
 *      Author: godinho
 */

#include "TLSLink.h"
#include "SimulationNode.h"
#include "NodeInput.h"
#include <log.h>

TLSLink::TLSLink(SimulationNode* from, SimulationNode* to, int meanFlush) : mFrom(from), mTo(to), mFlush(meanFlush){
	mIntersectionNode = 0;
}

TLSLink::~TLSLink() {
}

SimulationNode *TLSLink::getFrom() const {
    return mFrom;
}

SimulationNode *TLSLink::getTo() const {
    return mTo;
}

int TLSLink::move(float ratio) {

	if(mIntersectionNode==0) {
		rsi::log("mesoscopic", __FILE__, __LINE__, "TLSLink not associated with an intersection.");
	}

	NodeInput* input = mIntersectionNode->getNodeInput(mFrom);

	if(input == 0) {
		rsi::log("mesoscopic", __FILE__, __LINE__,
		    "Invalid target node on link. Check if this link was registered on target (from: %s to: %s) ",
		    mFrom->getId().c_str(), mTo->getId().c_str());
		return 0;
	}

	int amount = input->getCarsToTransfer() * ratio;
	input->setAmountTransfered(amount);

	NodeInput* destine = mTo->getNodeInput(mIntersectionNode);

	if(destine == 0) {
		rsi::log("mesoscopic", __FILE__, __LINE__, "Cannot find the mother fucking queue on destine node.");
		return 0;
	}

	destine->addCarsOnTrip(amount);

	return amount;
}

void TLSLink::setIntersectionNode(SimulationNode* intersectionNode) {
	 mIntersectionNode = intersectionNode;
}

int TLSLink::getFlush() {
	return mFlush;
}


